﻿Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices

Public Class RCMS_Container
#Region "Private declarations"
    Private CA As CLDS_RCMS
    Private building As String = "-"
    Private campus As String = "-"
    Private floor As String = "-"
    Private room As String = "-"
    Private mode As String = ""
    Private model As String = "-"
    Private xfpid As String = "-"
    Private sortColumnFP As Integer = -1
    Private sortColumnCB As Integer = -1
#End Region
#Region "Public properties"
    Public Property pCA() As CLDS_RCMS
        Get
            pCA = CA
        End Get
        Set(ByVal value As CLDS_RCMS)
            CA = value
        End Set
    End Property
    Public Property pBuilding() As String
        Get
            Return building
        End Get
        Set(ByVal value As String)
            building = value
        End Set
    End Property
    Public Property pCampus() As String
        Get
            Return campus
        End Get
        Set(ByVal value As String)
            campus = value
        End Set
    End Property

    Public Property pFloor() As String
        Get
            Return floor
        End Get
        Set(ByVal value As String)
            floor = value
        End Set
    End Property
    Public Property pRoom() As String
        Get
            Return room
        End Get
        Set(ByVal value As String)
            room = value
        End Set
    End Property
    Public Property pMode() As String
        Get
            Return mode
        End Get
        Set(ByVal value As String)
            mode = value
        End Set
    End Property
    Public Property pModel() As String
        Get
            Return model
        End Get
        Set(ByVal value As String)
            model = value
        End Set
    End Property
    Public Property pFPID() As String
        Get
            Return xfpid
        End Get
        Set(ByVal value As String)
            xfpid = value
        End Set
    End Property
#End Region
#Region "Initialisation"
    Public Sub Initialize(ByVal p_CA As CLDS_RCMS)
        Me.pgRCMS.SelectedObject = p_CA.pPrefs
        pCA = p_CA
        Me.btSearch.Enabled = True
        Me.btAddWP.Enabled = True
        Me.btEditWP.Enabled = False
        Me.btDeleteWP.Enabled = False
        Me.btCancel.Enabled = False
        Me.btSave.Enabled = False
        Me.btRange.Enabled = True
        Me.btZone.Enabled = True
        Me.btSync.Enabled = True
        Me.btList.Enabled = False
        Me.btExcel.Enabled = False
        Me.cxBuilding.Enabled = False
        Me.cxFloor.Enabled = False
        Me.cxRoom.Enabled = False
        Me.cxModel.Enabled = False
        Me.udFPNo.ReadOnly = True
        Me.tbProject.ReadOnly = True
        Me.tbPhase.ReadOnly = True
        Me.pBuilding = CA.CLDS_GetBuilding(Me)
        pCA.CLDS_RefreshLists(Me)
        If Me.pBuilding = "-" Then
            Me.cxBuilding.Visible = True
            Me.tbBuilding.Visible = False
        Else
            Me.cxBuilding.Visible = False
            Me.tbBuilding.Visible = True
            Me.tbBuilding.Text = Me.cxBuilding.Text
            pCA.CLDS_GetFloors(Me)
            Me.btList.Enabled = True
            Me.btExcel.Enabled = True
        End If
    End Sub
#End Region
#Region "Event handlers"
    Private Function InsertText(ByVal p_tb As System.Windows.Forms.TextBox, ByVal p_chr As Char) As String
        Dim cv As String = p_tb.Text
        Dim ip As Integer = p_tb.SelectionStart
        Dim it As String = ""
        If ip > 0 Then
            it = Mid$(cv, 1, ip)
        End If
        it = it & p_chr
        If ip > 0 Then
            it = it & Mid$(cv, ip + 1)
        End If
        'Debug.Print(cv & "," & ip & "," & it & Chr(10))
        Return it
    End Function
    Private Sub cableTextboxKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
        Handles tbCable01.KeyPress, tbCable02.KeyPress, tbCable03.KeyPress, tbCable04.KeyPress, tbCable05.KeyPress, _
        tbCable06.KeyPress, tbCable07.KeyPress, tbCable08.KeyPress, tbCable09.KeyPress, tbCable10.KeyPress
        Dim tb As System.Windows.Forms.TextBox = CType(sender, System.Windows.Forms.TextBox)
        If tb.ReadOnly Then
            tb.BackColor = System.Drawing.SystemColors.Control
        Else
            tb.BackColor = System.Drawing.Color.White
        End If
        tb.ForeColor = System.Drawing.SystemColors.WindowText
        Dim chr As Char = e.KeyChar
        If IsNumeric(e.KeyChar) Then
            'e.Handled = Not CA.CLDS_ValidCableID(tb.Text & chr)
            e.Handled = Not CA.CLDS_ValidCableID(InsertText(tb, chr))
        ElseIf Asc(UCase(chr)) >= Asc("A") And Asc(UCase(chr)) <= Asc("Z") Then
            'e.Handled = Not CA.CLDS_ValidCableID(tb.Text & chr)
            e.Handled = Not CA.CLDS_ValidCableID(InsertText(tb, chr))
        ElseIf Not Char.IsControl(e.KeyChar) Then
            'IsControl is checked, because without that, keys like BackSpace couldn't
            ' work correctly.
            e.Handled = True
        End If
    End Sub
    Private Sub btSavePrefs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSavePrefs.Click
        CA.pPrefs.SavePreferences()
        CA.CLDS_ColourLayerByPrefs()
        Me.pgRCMS.SelectedObject = CA.pPrefs
        Me.btSavePrefs.Enabled = False
        Me.pgRCMS.Update()
    End Sub
    Private Sub pgRCMS_PropertyValueChanged(ByVal s As Object, ByVal e As System.Windows.Forms.PropertyValueChangedEventArgs) Handles pgRCMS.PropertyValueChanged
        Me.btSavePrefs.Enabled = True
    End Sub
    Private Sub btConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btConnect.Click
        CA.pPrefs.LoadPreferences()
        lbConnected.Text = "Connecting..."
        lbConnected.Update()
        If CA.CLDS_URLConnection(True) Then
            CA.CLDS_GetProjectDefaults()
            If CA.pTokens.Count = 0 Then
                lbConnected.Text = "Disconnected"
            Else
                '                CA.CLDS_RefreshLists()
                If CA.pProps.v_projectname = "" Then
                    lbConnected.Text = "Connected"
                Else
                    lbConnected.Text = "Connected to " & UCase(CA.pProps.v_projectname)
                End If
            End If
        Else
            lbConnected.Text = "Disconnected"
        End If
        lbConnected.Update()
    End Sub
    Private Sub btEditWP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btEditWP.Click
        If Me.lvFaceplates.Visible Then
            CA.CLDS_ShowList(Me, True)
        End If
        CA.CLDS_UpdateWP(Me)
    End Sub
    Private Sub btAddWP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btAddWP.Click
        mode = "ADD"
        If Me.lvFaceplates.Visible Then
            CA.CLDS_ShowList(Me, True)
        End If
        CA.CLDS_AddWP(Me)
    End Sub
    Private Sub cxFloor_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cxFloor.SelectedValueChanged
        If Me.pFloor <> Me.cxFloor.Text Then
            If mode = "ADD" Or mode = "NEW" Then
                Me.pFloor = Me.cxFloor.Text
                Me.cxRoom.Text = ""
                Me.pRoom = ""
                CA.CLDS_GetRooms(Me)
                Me.udFPNo.Value = 0
                Me.tbFPID.Text = ""
            End If
        End If
    End Sub
    Private Sub cxFloor_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cxFloor.TextChanged
        If Me.pFloor <> Me.cxFloor.Text Then
            If mode = "ADD" Or mode = "NEW" Then
                Me.pFloor = Me.cxFloor.Text
                Me.cxRoom.Text = ""
                Me.pRoom = ""
                CA.CLDS_GetRooms(Me)
                Me.udFPNo.Value = 0
                Me.tbFPID.Text = ""
            End If
        End If
    End Sub
    Private Sub cxRoom_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cxRoom.SelectedIndexChanged
        If Me.pRoom <> Me.cxRoom.Text Then
            Me.pRoom = Me.cxRoom.Text
            If mode = "ADD" Or mode = "NEW" Then
                CA.CLDS_NextFP(Me)
            End If
        End If
    End Sub
    Private Sub cxRoom_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cxRoom.TextChanged
        If Me.pRoom <> Me.cxRoom.Text Then
            Me.pRoom = Me.cxRoom.Text
            If mode = "ADD" Or mode = "NEW" Then
                CA.CLDS_NextFP(Me)
            End If
        End If
    End Sub

    Private Sub btCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCancel.Click
        CA.CLDS_CancelWP(Me)
        mode = ""
    End Sub
    Private Sub btSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSearch.Click
        '        If mode = "" Then
        'CA.CLDS_EnterQuery(Me)
        '       Else
        CA.CLDS_ExecuteQuery(Me)
        '       End If
    End Sub
    Private Sub btSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSave.Click
        CA.CLDS_SaveWP(Me)
    End Sub
    Private Sub btAutofill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btAutofill.Click
        CA.CLDS_Autofill(Me)
    End Sub
    Private Sub btZone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btZone.Click
        CA.CLDS_PointZone(Me)
    End Sub
    Private Sub btRange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btRange.Click
        CA.CLDS_RangeTable()
    End Sub
    Private Sub btSync_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSync.Click
        CA.CLDS_SyncAll()
    End Sub
    Private Sub btList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btList.Click
        CA.CLDS_ShowList(Me, True)
    End Sub
    Private Sub lvFaceplates_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lvFaceplates.ColumnClick
        Dim lv As System.Windows.Forms.ListView = TryCast(sender, System.Windows.Forms.ListView)
        If Not lv Is Nothing Then
            If e.Column = sortColumnFP Then
                If lv.Sorting = Windows.Forms.SortOrder.Ascending Then
                    lv.Sorting = Windows.Forms.SortOrder.Descending
                Else
                    lv.Sorting = Windows.Forms.SortOrder.Ascending
                End If
            Else
                sortColumnFP = e.Column
                lv.Sorting = Windows.Forms.SortOrder.Ascending
            End If
            lv.ListViewItemSorter = New RCMS_FPLVSorter(e.Column, lv.Sorting)
            lv.Sort()
        End If
    End Sub
    Private Sub lvFaceplates_ItemSelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles lvFaceplates.ItemSelectionChanged
        Dim lv As System.Windows.Forms.ListView = TryCast(sender, System.Windows.Forms.ListView)
        If Not lv Is Nothing Then
            Dim wpL As List(Of ObjectId) = New List(Of ObjectId)
            CA.CLDS_SelectWPsByLVFP(lv, wpL)
            Debug.Print("Got list by FP " & Str$(wpL.Count))
            CA.CLDS_ShowSelectedWPs(Me, wpL)
            'If wpL.Count = 1 Then
            'CA.CLDS_ShowWP(Me, lv.SelectedItems(0).Text)
            'Else
            'CA.CLDS_ShowWP(Me, "")
            'End If
        End If
    End Sub
    Private Sub lvCables_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lvCables.ColumnClick
        Dim lv As System.Windows.Forms.ListView = TryCast(sender, System.Windows.Forms.ListView)
        If Not lv Is Nothing Then
            If e.Column = sortColumnCB Then
                If lv.Sorting = Windows.Forms.SortOrder.Ascending Then
                    lv.Sorting = Windows.Forms.SortOrder.Descending
                Else
                    lv.Sorting = Windows.Forms.SortOrder.Ascending
                End If
            Else
                sortColumnCB = e.Column
                lv.Sorting = Windows.Forms.SortOrder.Ascending
            End If
            lv.ListViewItemSorter = New RCMS_CBLVSorter(e.Column, lv.Sorting)
            lv.Sort()
        End If
    End Sub
    Private Sub lvCables_ItemSelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles lvCables.ItemSelectionChanged
        Dim lv As System.Windows.Forms.ListView = TryCast(sender, System.Windows.Forms.ListView)
        If Not lv Is Nothing Then
            Dim wpL As List(Of ObjectId) = New List(Of ObjectId)
            CA.CLDS_SelectWPsByLVCable(lv, wpL)
            Debug.Print("Got list by cable " & Str$(wpL.Count))
            CA.CLDS_ShowSelectedWPs(Me, wpL)
            'If lv.SelectedItems.Count = 1 Then
            'CA.CLDS_ShowWP(Me, lv.SelectedItems(0).SubItems(1).Text)
            'Else
            'CA.CLDS_ShowWP(Me, "")
            'End If
        End If
    End Sub
    Private Sub udFPNo_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles udFPNo.ValueChanged
        Dim r As Boolean = CA.CLDS_CheckDupFP(Me)
    End Sub
    Private Sub btDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btDeleteWP.Click
        CA.CLDS_DeleteWP(Me)
    End Sub
    Private Sub btExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btExcel.Click
        CA.CLDS_Excel_Report(Me)
    End Sub
    Private Sub tcRCMS_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles tcRCMS.Selected
        If CA Is Nothing Then
            Exit Sub
        End If
        If Me.pBuilding Is Nothing OrElse Me.pBuilding = "" OrElse Me.pBuilding = "-" Then
            Exit Sub
        End If
        Dim tc As System.Windows.Forms.TabControl = TryCast(sender, System.Windows.Forms.TabControl)
        If Not tc Is Nothing Then
            Dim tp As System.Windows.Forms.TabPage = tc.SelectedTab
            If tp.Name = "tpReports" Then
                Dim cFloor As Collection = CA.CLDS_PopulateCollection("FACEPLATE", "FLOOR", "CODE", "", Me.pBuilding)
                CA.CLDS_PopulateCB(Me.cxScopeFloor, cFloor, Me.pFloor, "~")
                Dim cProject As Collection = CA.CLDS_PopulateCollection("FACEPLATE", "PROJECT", "CODE", "", Me.pBuilding)
                CA.CLDS_PopulateCB(Me.cxFilterProject, cProject, "", "~")
                Dim cPhase As Collection = CA.CLDS_PopulateCollection("FACEPLATE", "PHASE", "CODE", "", Me.pBuilding)
                CA.CLDS_PopulateCB(Me.cxFilterPhase, cPhase, "", "~")
                Me.rbScopeDrawing.Checked = True
                Me.cxScopeFloor.Visible = False
            End If
        End If
    End Sub
    Private Sub rbScope_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbScopeBuilding.CheckedChanged, rbScopeDrawing.CheckedChanged, rbScopeFloor.CheckedChanged
        Dim rb As System.Windows.Forms.RadioButton = TryCast(sender, System.Windows.Forms.RadioButton)
        If rb.Checked Then
            If rb.Name = "rbScopeFloor" Then
                Me.cxScopeFloor.Visible = True
                Me.cxScopeFloor.Text = Me.pFloor
            Else
                Me.cxScopeFloor.Visible = False
            End If
        End If
    End Sub
    Private Sub cxModel_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles cxModel.DrawItem

        Dim cb As System.Windows.Forms.ComboBox = DirectCast(sender, System.Windows.Forms.ComboBox)

        e.DrawBackground()

        Dim myItem As CLDS_ColourComboItem = DirectCast(cxModel.Items(e.Index), CLDS_ColourComboItem)

        Dim ComboFBrush As System.Drawing.Brush
        Dim ComboBBrush As System.Drawing.Brush
        If cb.Enabled Then
            If (e.State And Windows.Forms.DrawItemState.Selected) = Windows.Forms.DrawItemState.Selected Then
                If myItem.BColor = Drawing.Color.White Then
                    ComboFBrush = New System.Drawing.SolidBrush(System.Drawing.Color.White)
                    ComboBBrush = New System.Drawing.SolidBrush(System.Drawing.Color.Blue)
                Else
                    ComboFBrush = New System.Drawing.SolidBrush(myItem.BColor)
                    ComboBBrush = New System.Drawing.SolidBrush(System.Drawing.Color.Blue)
                End If
            Else
                ComboBBrush = New System.Drawing.SolidBrush(myItem.BColor)
                ComboFBrush = New System.Drawing.SolidBrush(myItem.FColor)
            End If
                Debug.Print(myItem.Text & "," & e.State)
            Else
                ComboBBrush = New System.Drawing.SolidBrush(System.Drawing.SystemColors.Control)
                ComboFBrush = New System.Drawing.SolidBrush(System.Drawing.SystemColors.WindowText)
            End If

        Dim FontToUse As System.Drawing.Font = e.Font
        If myItem.Bold Then
            FontToUse = New System.Drawing.Font(e.Font, System.Drawing.FontStyle.Bold)
        End If

        e.Graphics.FillRectangle(ComboBBrush, e.Bounds)
        e.Graphics.DrawString(myItem.Text, FontToUse, ComboFBrush, e.Bounds)

        e.DrawFocusRectangle()
    End Sub
    Private Sub cxModel_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cxModel.EnabledChanged
        Dim cb As System.Windows.Forms.ComboBox = DirectCast(sender, System.Windows.Forms.ComboBox)
        If cb.Enabled Then
            For Each wp As CLDS_ColourComboItem In cb.Items
                wp.BColor = System.Drawing.Color.White
                wp.FColor = System.Drawing.SystemColors.WindowText
            Next
        End If
    End Sub
#End Region

End Class
